using System;
using System.Collections.Generic;
using Hammock;
using WWB.DisqusSharp.Model.DisqusService;

namespace WWB.DisqusSharp.Infrastructure.HammockWrappers
{
    public static class DisqusRestRequestExtensions
    {
        public static RestRequest ForForum(this RestRequest request, string forum_id)
        {
            request.AddParameter("forum_id", forum_id);
            return request;
        }

        public static RestRequest ForThread(this RestRequest request, string thread_id)
        {
            request.AddParameter("thread_id", thread_id);
            return request;
        }

        public static RestRequest StartingAt(this RestRequest request, int startAt)
        {
            request.AddParameter("start", startAt.ToString());
            return request;
        }

        public static RestRequest StartingAtAndLimiting(this RestRequest request, StartLimitArgs startLimit)
        {
            var sl = startLimit ?? new StartLimitArgs();
            return request.StartingAt(sl.Start).LimitTo(sl.Limit);
        }

        public static RestRequest LimitTo(this RestRequest request, int limit)
        {
            request.AddParameter("limit", limit.ToString());
            return request;
        }

        public static RestRequest WithMultiValueStringParameter(this RestRequest request, string paramName, IEnumerable<string> values)
        {
            var vals = values ?? new string[0];
            string filter = string.Join(",", vals);
            return request.WithParameterIfExists(paramName, filter);
        }

        public static RestRequest WithParameterIfExists(this RestRequest request, string paramName, string paramValue)
        {
            if (!string.IsNullOrWhiteSpace(paramValue))
            {
                request.AddParameter(paramName, paramValue);
            }
            return request;
        }

        public static RestRequest WithParameter(this RestRequest request, string paramName, string paramValue)
        {
            request.AddParameter(paramName, paramValue);
            return request;
        }

        public static RestRequest WithFieldIfExists(this RestRequest request, string fieldName, string fieldValue)
        {
            if (!string.IsNullOrWhiteSpace(fieldValue))
            {
                request.AddField(fieldName, fieldValue);
            }
            return request;
        }

        public static RestRequest WithField(this RestRequest request, string fieldName, string fieldValue)
        {
            request.AddField(fieldName, fieldValue);
            return request;
        }

        public static string ToDisqusPostTimeFormat(this DateTime when)
        {
            return when.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm");
        }
    }
}